﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App_Code;

public partial class search : System.Web.UI.Page
{
    dulieuDataContext db = new dulieuDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //var nhom = (Convert.ToInt32(Request.QueryString["nhom-san-pham"]));
            var hang = (Convert.ToInt32(Request.QueryString["hang-san-xuat"]));
            var gia = (Request.QueryString["gia"]);
            LoadData(gia, hang);
        }

    }

    protected void LoadData(string gia, int hang)
    {
        collect.PageSize = 30;
        if (gia == null || gia == "0")
        {
            collect.DataSource = (from o in db.Products
                                  where o.Cate_ID == hang
                                  select o).ToList();
            collect.BindToControl = rpt_san_pham_ban_chay;
            rpt_san_pham_ban_chay.DataSource = collect.DataSourcePaged;
        }
        else
        {
            string[] arr = gia.Split('-');
            switch (arr[0].ToUpper())
            {
                case "<":
                    collect.DataSource = (from o in db.Products
                                          where o.Cate_ID == hang && o.PriceOnline <= Convert.ToInt32(arr[1]) * 1000
                                          select o).ToList();
                    break;
                case "BETWEEN":
                    collect.DataSource = (from o in db.Products
                                          where o.Cate_ID == hang && (o.PriceOnline >= Convert.ToInt32(arr[1]) * 1000 && o.PriceOnline <= Convert.ToInt32(arr[2]) * 1000)
                                          select o).ToList();
                    break;
                case ">":
                    collect.DataSource = (from o in db.Products
                                          where o.Cate_ID == hang && o.PriceOnline >= Convert.ToInt32(arr[1]) * 1000
                                          select o).ToList();
                    break;
            }

            collect.BindToControl = rpt_san_pham_ban_chay;
            rpt_san_pham_ban_chay.DataSource = collect.DataSourcePaged;
        }
    }
    protected void BuyClick(object s, CommandEventArgs e)
    {
        var idpr = e.CommandName;
        var cart = (Cart)Session["cart"];
        if (cart == null)
        {
            cart = new Cart();
        }
        cart.AddPro(idpr, 1);
        Session.Add("cart", cart);
        Response.Write("<SCRIPT language='JavaScript'>alert('Đã thêm vào giỏ hàng!');</SCRIPT> ");
    }
}